<?php
// lib/model/doctrine/JobeetAffiliate.class.php
class JobeetAffiliate extends BaseJobeetAffiliate
{

    public function getActiveJobs(array $parameters = null)
      {
        $q = Doctrine_Query::create()
          ->select('j.*')
          ->from('JobeetJob j')
          ->leftJoin('j.JobeetCategory c')
          ->leftJoin('c.JobeetAffiliates a')
          ->where('a.id = ?', $this->getId());

        if(isset($parameters['category']))
        {
            $q->andWhere('c.name = ?', $parameters['category']);
        }
        
        if(isset($parameters['limit']))
        {
            $q->limit($parameters['limit']);
        }
        
        $q = Doctrine_Core::getTable('JobeetJob')->addActiveJobsQuery($q);

        return $q->execute();
      }

  public function save(Doctrine_Connection $conn = null)
  {
    if (!$this->getToken())
    {
      $this->setToken(sha1($this->getEmail().rand(11111, 99999)));
    }
 
    return parent::save($conn);
  }
  
  public function activate()
  {
    $this->setIsActive(true);
 
    return $this->save();
  }
 
  public function deactivate()
  {
    $this->setIsActive(false);
 
    return $this->save();
  }

 
}